﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MBEditor
{
    partial class Editor
    {
#region Private Members
        private SortedDictionary<int, string> storedLines;
        private ConsoleColor foreground;
        private ConsoleColor background;

        public static readonly int MB_MAGIC = 987654321;

        private static OpenFileDialog opener = null;
        private static SaveFileDialog saver = null;

        static private void Display(ConsoleColor newFore, ConsoleColor newBack, string message)
        {
            ConsoleColor currentFore = Console.ForegroundColor;
            ConsoleColor currentBack = Console.BackgroundColor;
            Console.ForegroundColor = newFore;
            Console.BackgroundColor = newBack;
            Console.Write(message);
            Console.ForegroundColor = currentFore;
            Console.BackgroundColor = currentBack;
        }

        static private void DisplayError(string error, bool newLine)
        {
            string message = "Error: " + error;
            if (newLine)
            {
                message += "\n";
            }
            Display(ConsoleColor.Red, ConsoleColor.Black, message);
        }

        static private void DisplayStatus(string status, bool newLine)
        {
            if (newLine)
            {
                status += "\n";
            }
            Display(ConsoleColor.Yellow, ConsoleColor.Black, status);
        }

        private void ResetConsoleColors()
        {
            Console.BackgroundColor = background;
            Console.ForegroundColor = foreground;
        }
#endregion

        public Editor()
        {
            background = Console.BackgroundColor;
            foreground = Console.ForegroundColor;
            storedLines = new SortedDictionary<int, string>();
            DisplayStatus("MiniBasic Interactive Editor - By Adrian Wright\nhttp://code.google.com/p/mini-basic/\n", true);
        }

        bool ParseLine(string inputLine)
        {
            bool shouldContinue = false;
            string inputLineCopy = inputLine;
            string firstWord = EditorUtils.TrimAndRemoveLowererFirstWord(ref inputLine);
            // if it's the exit command, then just exit
            if (firstWord != "bye")
            {
                // else see it is a built-in command
                if(!TestIfAndCallBuiltinFunc(firstWord, inputLine))
                {
                    // if it's not, see if it's a number
                    int lineNum;
                    if (Int32.TryParse(firstWord, out lineNum))
                    {
                        // it is, take it as a line number and line
                        // if the line exists, replace it, otherwise just add it
                        if(storedLines.ContainsKey(lineNum))
                        {
                            storedLines.Remove(lineNum);
                        }
                        storedLines.Add(lineNum, inputLine);
                    }
                    else
                    {
                        // if we haven't processed it so far
                        // take it as an interactive program fragment

                        // filter out lines consisting solely of spaces
                        inputLineCopy = inputLineCopy.Trim();
                        if (inputLineCopy.Length > 0)
                        {
                            // prepend a line number and add on a newline
                            inputLineCopy = inputLineCopy.Insert(0, "1 ");
                            inputLineCopy += '\r';
                            RunString(inputLineCopy);
                            ResetConsoleColors();
                        }
                    }
                }
                shouldContinue = true;
            }
            return shouldContinue;
        }

        public void Run()
        {
            string inputString = null;
            Console.Write("> ");
            while ((inputString = Console.ReadLine()) != null)
            {
                if (String.IsNullOrEmpty(inputString) || ParseLine(inputString))
                {
                    Console.Write("> ");
                }
                else break;
            }
        }
    }
}
